// featurePackage/pages/changePhone/changePhone.js
const app = getApp();
import {
  verifyOldPhone,
  bindNewPhone,
  getSmsCode,
} from "../../../service/jb_common_service"
import {
  bindAiPayInfo,
  updateAiPayInfo,
} from "../../../service/jb_mine_service"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    step: 0,//第一步 验证老手机号， 1：第二步，绑定新手机号
    oldPhone: "",
    oldCode: "",
    newPhone: "",
    newCode: "",
    aliRealName:"",
    smsCode:"",
    aliAccount:"",
    couponCodeBtnStatus: false,
    couponCodeBtnTitle: "获取验证码",
    snsMsgWait: 60,//倒计时
    timeInter: null,
    btnTitle:"绑定支付宝信息",//更新支付宝信息
    tempPhone:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     if(app.globalData.userInfo.aliAccount){
        this.setData({
          aliAccount:app.globalData.userInfo.aliAccount,
          aliRealName:app.globalData.userInfo.aliRealName,
          btnTitle:"更新支付宝信息"
        })
     }
     if(app.globalData.userInfo.phone){
       this.setData({
         tempPhone:app.globalData.userInfo.phone,
       })
     }
  
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  cellInputChange(e) {
    let value = e.detail.value;
    let key = e.currentTarget.dataset.key;
    if (key == 'aliRealName') {
      this.setData({
        aliRealName: value,
      })
    } else if (key == 'aliAccount') {
      this.setData({
        aliAccount: value,
      })
    } else if (key == 'smsCode') {
      this.setData({
        smsCode: value
      })
    }else if(key == 'tempPhone'){
      this.setData({
        tempPhone:value,
      })
    }
  },
  newPhoneAction(e){
    this.setData({
      newPhone:e.detail.value
    })
  },

  //发送验证码
  clickSendCode(e) {
    let type = e.currentTarget.dataset.type;

    if (this.data.couponCodeBtnStatus) {
      return;
    }
    let that = this;
    //点击了发送按钮，就先禁用掉这个按钮点击
    this.setData({
      couponCodeBtnStatus: true,
    })
    //调用发送验证码接口:
    let phone = "";
    if (type == 'old') {
      phone = this.data.tempPhone;
    } else {
      phone = this.data.newPhone;
    }

    let warn = null;
    if (phone == '') {
      warn = "号码不能为空";
    } else if (phone.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(phone)) {
      warn = "手机号格式不正确";
    } else {
      getSmsCode(phone).then((data) => {
        //当手机号正确的时候提示用户短信验证码已经发送
        wx.showToast({
          title: '短信验证码已发送',
          icon: 'none',
          duration: 2000
        });
        let inter = setInterval(function () {
          this.setData({
            couponCodeBtnStatus: true,
            couponCodeBtnTitle: this.data.snsMsgWait + 's后重发',
            snsMsgWait: this.data.snsMsgWait - 1,
            timeInter: inter
          });
          if (this.data.snsMsgWait < 0) {
            clearInterval(inter)
            this.setData({
              couponCodeBtnTitle: '重新发送',
              snsMsgWait: 60,
              couponCodeBtnStatus: false
            });
          }
        }.bind(this), 1000);

      }).
        catch((err) => {
          that.setData({
            couponCodeBtnStatus: false
          })
          wx.showToast({
            icon: 'none',
            title: err,
          })
        })


    }
    if (warn != null) {
      wx.showModal({
        title: '提示',
        content: warn
      })
      that.setData({
        couponCodeBtnStatus: false
      })
      return;
    }


  },


  // 绑定新手机号
  bindNewPhoneAction() {
    if (!this.data.newPhone.trim() || this.data.newPhone.length != 11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: "none"
      })
      return;
    }
    if (!this.data.newCode.trim()) {
      wx.showToast({
        title: '请输入验证码',
        icon: "none"
      })
      return;
    }
    wx.showLoading();
    bindNewPhone({
      phone: this.data.newPhone,
      captcha: this.data.newCode,
    }).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '绑定成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/home/home',
          })
        }, 2000)
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "绑定失败",
        icon: "none"
      })

    })

  },
  //验证旧手机号
  verifyOldPhoneAction() {
    if (!this.data.aliRealName.trim()) {
      wx.showToast({
        title: '请输入真实姓名',
        icon: "none"
      })
      return;
    }
    // if (!this.data.aliAccount.trim() || this.data.aliAccount.length != 11) {
    //   wx.showToast({
    //     title: '请输入正确的手机号',
    //     icon: "none"
    //   })
    //   return;
    // }
    if (!this.data.smsCode.trim()) {
      wx.showToast({
        title: '请输入验证码',
        icon: "none"
      })
      return;
    }
    wx.showLoading();
    updateAiPayInfo({
      aliRealName: this.data.aliRealName,
      aliAccount: this.data.aliAccount,
      smsCode:this.data.smsCode
    }).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        let sucTitle = "绑定成功"
        if(this.data.btnTitle != '绑定支付宝信息'){
           sucTitle = '更新成功'
        }
        app.globalData.userInfo.aliAccount = this.data.aliAccount;
        app.globalData.userInfo.aliRealName = this.data.aliRealName;
        wx.showToast({
          title: sucTitle,
          icon:"success"
        })
        setTimeout(() => {
          wx.navigateBack();
        },2000)
        
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "验证失败",
        icon: "none"
      })

    })


  },




})